package UI;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Login extends JFrame {
    private JLabel message;

    public Login() {
        //创建此窗口对象时,马上调用init()进行初始化操作
        init();
    }



    /** 1.初始化界面组件和布局的 */
    private void init() {
        this.setTitle("登录系统");
        //2.创建内容窗格方法的对象
        JPanel contentPane = createContentPane();
        this.setContentPane(contentPane);
        this.setSize(300, 220);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                changeUI.exit();
            }
        });
    }

    /**创建内容窗格*/
    private JPanel createContentPane() {
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(new EmptyBorder(8,8,8,8));
         /* 整体布局分为三部分:上中下,所以使用边框布局BorderLayout
            上:一个单独文本标签,可直接输入
            中:两个框: 文本框/密码框
            下:两个按钮
        */
        p.add(BorderLayout.NORTH,new JLabel
                ("登录考试系统", JLabel.CENTER));
        p.add(BorderLayout.CENTER, createCenterPane());
        p.add(BorderLayout.SOUTH, createBtnPane());
        return p;
    }

    public void setChangeUI(ChangeUI changeUI) {
        this.changeUI = changeUI;
    }

    private ChangeUI changeUI;
    /**创建两个按钮Login 和 Cancel*/
    private JPanel createBtnPane() {
        JPanel p = new JPanel();
        JButton login = new JButton("Login");
        JButton cancel = new JButton("Cancel");
        p.add(login);
        p.add(cancel);
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeUI.login();
            }
        });
        return p;
    }

    private JPanel createCenterPane() {
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(new EmptyBorder(8, 0, 0, 0));
        p.add(BorderLayout.NORTH, createIdPwdPane());
        message = new JLabel("", JLabel.CENTER);
        p.add(message, BorderLayout.CENTER);
        return p;
    }

    private JPanel createIdPwdPane() {
        JPanel p = new JPanel(new GridLayout(2, 1, 0, 6));
        //添加ID
        p.add(createIdPane());
        //添加密码
        p.add(createPwdPane());
        return p;
    }
    private JPasswordField pwdField;
    private JPanel createPwdPane() {
        JPanel p = new JPanel(new BorderLayout(5,0));
        p.add(BorderLayout.WEST, new JLabel("密码:"));
        pwdField = new JPasswordField();
        //设置支持输入法,如果不支持输入字符数可见的
        //pwdField.enableInputMethods(true);
        p.add(BorderLayout.CENTER, pwdField);
        /*this.pwdField = pwdField;*/
        return p;
    }
    private JTextField idField;
    private JPanel createIdPane() {
        JPanel p = new JPanel(new BorderLayout(5,0));
        p.add(BorderLayout.WEST, new JLabel("账号:"));
        idField = new JTextField();
        //设置支持输入法,如果不支持输入字符数可见的
        //pwdField.enableInputMethods(true);
        p.add(BorderLayout.CENTER, idField);
        return p;
    }

    public int getUserId() {
        int id= Integer.parseInt(idField.getText());
        return id;
    }

    public String getUserPwd() {
        char[] pwd = pwdField.getPassword();
        return new String(pwd);
    }

    public void showError(String message) {
        this.message.setText(message);
    }
}
